﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace YXShop.Web.membercenter.product
{
    public partial class onlinesignupinfo_list : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (ChangeHope.WebPage.PageRequest.GetFormString("Option") != string.Empty && ChangeHope.WebPage.PageRequest.GetFormInt("id") > 0)
                {
                    string types = Request.Form["Option"].Trim();
                    int id = ChangeHope.WebPage.PageRequest.GetFormInt("id");
                    if (types == "del")
                    {
                        del(id);
                    }
                    Response.End();
                    return;
                }
                this.returnList.NavigateUrl = "groupbuyinfo_list.aspx";
                GetList();
            }
        }
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        protected void GetList()
        {
            YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();
            ChangeHope.WebPage.Table table = new ChangeHope.WebPage.Table();
            YXShop.BLL.Product.GroupBuy_OnlineSignUpInfo data = new YXShop.BLL.Product.GroupBuy_OnlineSignUpInfo();
            ChangeHope.DataBase.DataByPage dataPage = data.GetList();
            DataTable dt = null;

            //查询发起团购信息
            YXShop.BLL.Product.Product_SendGroupBuyInfo psbll = new YXShop.BLL.Product.Product_SendGroupBuyInfo();
            YXShop.Model.Product.Product_SendGroupBuyInfo psmodel = null;
            //查询团购商品
            YXShop.BLL.Product.Product_IntegratePurchas pibll = new YXShop.BLL.Product.Product_IntegratePurchas();
            YXShop.Model.Product.Product_IntegratePurchas pimodel = null;
            //第一步先添加表头
            table.AddHeadCol("10%", "编号");
            table.AddHeadCol("20%", "报名人");
            table.AddHeadCol("15%", "人数");
            table.AddHeadCol("20%", "报名时间");
            table.AddHeadCol("15%", "报名状态");
            table.AddHeadCol("20%", "操作");
            table.AddRow();
            //添加表的内容
            if (dataPage.DataReader != null)
            {
                while (dataPage.DataReader.Read())
                {
                    string strOrder = "<span style='color:#666'>转成订单</span>";
                    psmodel=psbll.GetModelID(Convert.ToInt32(dataPage.DataReader["sid"].ToString()));
                    if (psmodel != null)
                    {
                        pimodel = pibll.GetModelProductID(psmodel.ProductID);

                        if (System.DateTime.Now > Convert.ToDateTime(psmodel.ActivityEndTime))
                        {
                            if (pimodel != null)
                            {

                                //查看报名人数
                                int count = 0;
                                dt = data.GetActivityList(dataPage.DataReader["sid"].ToString());
                                if (dt.Rows.Count > 0)
                                {
                                    for (int i = 0; i < dt.Rows.Count; i++)
                                    {
                                        count += Convert.ToInt32(dt.Rows[i]["signupcount"].ToString());
                                    }
                                }

                                string[] LadderCount = pimodel.LadderCount.Split(',');
                                string[] SortLadderCount = ChangeHope.Common.SortHelper.BubbleSort(LadderCount);
                                for (int f = SortLadderCount.Length-1; f >=0; f--)
                                {
                                    if (Convert.ToInt32(SortLadderCount[f]) <= count)
                                    {
                                        strOrder = "<a href='make_convert_orders.aspx?SendGroupBuyId=" + dataPage.DataReader["sid"].ToString() + "&GroupBuySignupId=" + dataPage.DataReader["id"].ToString() + "&ConvertOrderType=2'>转成订单</a>";
                                        break;
                                    }
                                }
                            }
                           
                        }
                    }
                    table.AddCol("No." + dataPage.DataReader["id"].ToString());
                    table.AddCol(dataPage.DataReader["name"].ToString());
                    table.AddCol(dataPage.DataReader["signupcount"].ToString());

                    table.AddCol(dataPage.DataReader["createtime"].ToString());
                    table.AddCol(dataPage.DataReader["state"].ToString() == "0" ? "未通过" : "通过");
                    table.AddCol(string.Format("" + strOrder + "  <a href='onlinesignupinfo.aspx?id={0}&productid={1}'>详情</a> <a href='#' onclick='Del({0})'>删除</a>", dataPage.DataReader["id"].ToString(), ChangeHope.WebPage.PageRequest.GetQueryString("q_productid")));
                    table.AddRow();
                }
            }
            string view = table.GetTable() + dataPage.PageToolBar;
            dataPage.Dispose();
            dataPage = null;
            this.litInfo.Text = view;
        }


        private void del(int id)
        {
            YXShop.BLL.Product.GroupBuy_OnlineSignUpInfo bll = new YXShop.BLL.Product.GroupBuy_OnlineSignUpInfo();
            bll.Delete(id);
            Response.Write("ok");
        }
    }
}

